import axios from 'axios'

let status = "DEV";


if (status == "DEV") {

  axios.defaults.baseURL = '';

} else if (status == "PROD") {
  axios.defaults.baseURL = 'http://60.223.235.71:8750/';
}


//设置默认请求头
axios.defaults.headers = {
  'Content-Type': 'application/json',
}
//设置默认超时时间
axios.defaults.timeout = 10000;

//请求拦截器  可在这里向请求头中添加数据 例如token
axios.interceptors.request.use(config => {
  const TOKEN = localStorage.getItem('token');
  if(/financial/.test(config.url)) {
    config.headers.Authorization = 'Bearer-eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NzcxNDk3MDksInVzZXJuYW1lIjoiYWRtaW4ifQ.nIPbEChzi5TwuvpD0ZcfxSlt0u3iK-fDqLO76uWaM38'
  } else if (/221.204.170.102/.test(config.url) && TOKEN) {
    config.headers.Authorization = `Bearer ${TOKEN}`
  } else if (/platforms/.test(config.url)) {
    // 平台监控token
    config.headers.Authorization = 'fjJYZNb83dD37Urm3ih4Xv6iFvVMjGVCCTE3zZqMG6WZn82qXmbKN6KthAaxoTftWzKjugQrdiDPT2inc1fREG4ITqVcQmo6DY2';
  }
  return config;
}, error => {
  return Promise.reject(error)
})


//响应拦截器 可进行异常处理
axios.interceptors.response.use(response => {
  return response;
}, err => {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = '错误请求'
        break;
      case 401:
        err.message = '未授权，请重新登录'
        break;
      case 403:
        err.message = '拒绝访问'
        break;
      case 404:
        err.message = '请求错误,未找到该资源'
        break;
      case 405:
        err.message = '请求方法未允许'
        break;
      case 408:
        err.message = '请求超时'
        break;
      case 500:
        err.message = '服务器端出错'
        break;
      case 501:
        err.message = '网络未实现'
        break;
      case 502:
        err.message = '网络错误'
        break;
      case 503:
        err.message = '服务不可用'
        break;
      case 504:
        err.message = '网络超时'
        break;
      case 505:
        err.message = 'http版本不支持该请求'
        break;
    }
  } else {
    err.message = "连接到服务器失败"
  }
  console.log(err.message);
  return Promise.resolve(err.response)
})


//封装具体的请求方法

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
        params: params
      })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err)
      })
  })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}, config = {}) {
  return new Promise((resolve, reject) => {
    // let dataStr = JSON.stringify(data);
    // let sign = md5(dataStr + "&key=" + tools.signCode);
    axios.post(url, data, config).then(response => {
      resolve(response);
      // let sign = response.data.sign + "";
// // delete response.data.sign;
// let result = response.data.result;
// // console.log(result + "&key=" + tools.signCode);
// // console.log(md5(result + "&key=" + tools.signCode));
// if (md5(result + "&key=" + tools.signCode) == sign) {
//   resolve(JSON.parse(result));
// } else {
//   console.log("数据加密校验失败");
//   resolve({
//     code: -1,
//     msg: "数据加密校验失败",
//     data: ""
//   })
// }
      // resolve(response.data);
    }, err => {
      reject(err)
    })
  })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}

export function jqGet(url, data = {}) {
  return new Promise((resolve, reject) => {
    $.ajax({
      type: 'GET',
      url:url,
      dataType: 'json',
      data: data,
      contentType:'application/x-www-form-urlencoded; charset=UTF-8',
      cache: false,
      success:function (res) {
        resolve(res);
      },
      error:function (err) {
        reject(err);
      },
      timeout:10000
    })
  })
}

export function jqPost(url, data = {}) {
  return new Promise((resolve, reject) => {
    $.ajax({
      type: 'POST',
      url:url,
      dataType: 'json',
      data: data,
      contentType:'application/x-www-form-urlencoded; charset=UTF-8',
      cache: false,
      success:function (res) {
        resolve(res);
      },
      error:function (err) {
        reject(err);
      },
      timeout:10000
    })
  })
}
